package edu.yzu.testspringboot002.utils;

import com.github.pagehelper.Page;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * mybatis 分页
 *
 * @author 余浪
 * @Date 2020/4/27
 */
@Data
@NoArgsConstructor
public class Pager<T> implements Serializable {

    // 当前页码
    private long pageNum = 1;
    // 每页的数量
    private long pageSize = 10;
    // 总页数
    private long total = 0;
    // 总分页
    private long pages = 0;
    // 是否有下一页
    private boolean hasMore = false;

    private List<T> list = Collections.emptyList();

    public Pager(List<T> list) {
        this.hasMore = false;
        if (list instanceof Page) {
            Page page = (Page) list;
            this.pageNum = page.getPageNum();
            this.pageSize = page.getPageSize();
            this.total = page.getTotal();
            this.pages = page.getPages();
            this.list = page.getResult();
        } else {
            this.pageNum = 1;
            this.pageSize = list.size();
            this.total = this.pageSize > 0 ? 1 : 0;
            this.pages = this.pageSize > 0 ? 1 : 0;
            this.list = list;
        }

        if (list instanceof Collection) {
            this.hasMore = this.total > this.pageSize;
        }
    }

    /**
     * 分页构造函数
     *
     * @param pageNum  当前页
     * @param pageSize 每页显示条数
     * @author 余浪
     */
    public Pager(long pageNum, long pageSize) {
        this(pageNum, pageSize, 0);
    }

    public Pager(long pageNum, long pageSize, long total) {
        if (pageNum > 1) {
            this.pageNum = pageNum;
        }
        this.pageSize = pageSize;
        this.total = total;
        this.hasMore = this.hasNext();
    }

    // 自定义分页
    public Pager(long pageNum, long pageSize, List<T> list) {
        if (pageNum > 1) {
            this.pageNum = pageNum;
        }
        this.setList(list);
        this.pageSize = pageSize;
        this.total = list.size();
        this.pages = (list.size() / pageSize + ((list.size() % pageSize == 0) ? 0 : 1));
        this.hasMore = this.hasNext();
    }

    public Pager(long pageNum, long pageSize, long total, long pages) {
        if (pageNum > 1) {
            this.pageNum = pageNum;
        }
        this.pageSize = pageSize;
        this.total = total;
        this.pages = pages;
        this.hasMore = this.hasNext();
    }

    public static <T> Pager<T> of(List<T> list) {
        return new Pager<>(list);
    }

    /**
     * 是否存在上一页
     *
     * @return true / false
     */
    public boolean hasPrevious() {
        return this.pageNum > 1;
    }

    /**
     * 是否存在下一页
     *
     * @return true / false
     */
    public boolean hasNext() {
        return this.total > this.pageSize;
    }

    public Pager<T> setList(List<T> list) {
        this.list = list;
        return this;
    }

    /**
     * 重写 分页
     * @author: 余浪
     * @Date: 2021/3/10 14:33
     * @Version: 1.0.0
     **/
    public Pager(long pageNum, long pageSize, long total, List list) {
        if (pageNum > 1) {
            this.pageNum = pageNum;
        }
        this.pageSize = pageSize;
        this.total = total;
        this.hasMore = this.hasNext();
        this.pages =  (list.size() / pageSize + ((list.size() % pageSize == 0) ? 0 : 1));
        this.list = list;
    }
}
